package ru.spbau.bytecode.gui;

import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

import ru.spbau.bytecode.graph.Vertex;

public class CheckBoxTreeNodeRenderer
		extends JCheckBox implements TreeCellRenderer{
	
	private DefaultTreeCellRenderer defaultRenderer =
		new DefaultTreeCellRenderer();
	
	public CheckBoxTreeNodeRenderer() {
		setOpaque(false);
	}

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean selected, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		
		if (!(value instanceof DefaultMutableTreeNode)) {
			return defaultRenderer.getTreeCellRendererComponent(
					tree, value, selected, expanded, leaf, row, hasFocus);
		}
		
		Object data = ((DefaultMutableTreeNode)value).getUserObject();
		
		if (!(data instanceof Vertex)) {
			return defaultRenderer.getTreeCellRendererComponent(
					tree, value, selected, expanded, leaf, row, hasFocus);
		}
		
		Vertex v = (Vertex)(data);
		setSelected(v.isVisible());
		setText(v.getName());
		return this;
	}

}
